<template>
  <el-dialog
    :title="!dataForm.storeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="所属分类" prop="classifyId">
        <!-- <el-input v-model="dataForm.classifyId" placeholder="${column.comments}"></el-input> -->
        <template>
          <el-select v-model="dataForm.classifyId" placeholder="请选择所属分类">
            <el-option
              v-for="item in classifies"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="dataForm.storeName"
          placeholder="填写店铺名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="店铺描述" prop="storeDesc">
        <textarea
          v-model="dataForm.storeDesc"
          style="margin: 5px; height: 226px; width: 96%"
          placeholder="填写店铺描述"
        ></textarea>
      </el-form-item>
      <el-form-item label="店铺公告" prop="storeAnno">
        <textarea
          v-model="dataForm.storeAnno"
          style="margin: 5px; height: 226px; width: 96%"
          placeholder="填写店铺公告"
        ></textarea>
      </el-form-item>
      <el-form-item label="店铺头像" prop="storeIcon">
        <single-upload v-model="dataForm.storeIcon"></single-upload>
      </el-form-item>
      <el-form-item label="店铺背景图" prop="backgroundUrl">
        <single-upload v-model="dataForm.backgroundUrl"></single-upload>
      </el-form-item>
      <el-form-item label="店铺所在区域" prop="storeLocation">
        <el-input
          v-model="dataForm.storeLocation"
          placeholder="店铺所在区域"
        ></el-input>
      </el-form-item>
      <el-form-item label="店铺详细地址" prop="storeAddress">
        <el-input
          v-model="dataForm.storeAddress"
          placeholder="店铺详细地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="店铺联系方式" prop="storePhone">
        <el-input
          v-model="dataForm.storePhone"
          placeholder="店铺联系方式"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="店铺状态" prop="storeStatus">
        <el-switch
          v-model="dataForm.storeStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>-->
      <!-- <el-form-item label="店铺诚信度" prop="storeIntegrity">
        <el-input v-model="dataForm.storeIntegrity" placeholder="${column.comments}"></el-input>
      </el-form-item>-->
      <el-form-item label="营业许可证" prop="businessLicense">
        <single-upload v-model="dataForm.businessLicense"></single-upload>
      </el-form-item>
      <el-form-item label="店铺招牌图" prop="storeSign">
        <single-upload v-model="dataForm.storeSign"></single-upload>
      </el-form-item>
      <el-form-item label="店铺装潢图" prop="storeFront">
        <single-upload v-model="dataForm.storeFront"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// import ueditor from "@/views/demo/ueditor";
import SingleUpload from "@/components/upload/singleUpload.vue";
export default {
  components: {
    SingleUpload,
  },
  data() {
    return {
      visible: false,
      classifies: [],
      dataForm: {
        storeId: 0,
        storekeeperId: 0,
        classifyId: "",
        storeName: "",
        storeDesc: "",
        storeAnno: "",
        storeIcon: "",
        backgroundUrl: "",
        storeLocation: "",
        storeAddress: "",
        storePhone: "",
        storeStatus: 0,
        storeIntegrity: "",
        businessLicense: "",
        storeSign: "",
        storeFront: "",
      },
      dataRule: {
        classifyId: [
          {
            required: true,
            message: "必须选择分类",
            trigger: "blur",
          },
        ],
        storeName: [
          {
            required: true,
            message: "店铺名称不能为空",
            trigger: "blur",
          },
        ],
        storeDesc: [
          {
            required: true,
            message: "店铺描述不能为空",
            trigger: "blur",
          },
        ],
        storeAnno: [
          {
            required: true,
            message: "店铺公告不能为空",
            trigger: "blur",
          },
        ],
        storeIcon: [
          {
            required: true,
            message: "头像不能为空",
            trigger: "blur",
          },
        ],
        backgroundUrl: [
          {
            required: true,
            message: "背景图不能为空",
            trigger: "blur",
          },
        ],
        storeLocation: [
          {
            required: true,
            message: "店铺位置不能为空",
            trigger: "blur",
          },
        ],
        storeAddress: [
          {
            required: true,
            message: "店铺地址不能为空",
            trigger: "blur",
          },
        ],
        storePhone: [
          {
            required: true,
            message: "店铺电话不能为空",
            trigger: "blur",
          },
        ],
        businessLicense: [
          {
            required: true,
            message: "必须上传营业执照",
            trigger: "blur",
          },
        ],
        storeSign: [
          {
            required: true,
            message: "店铺招牌图不能为空",
            trigger: "blur",
          },
        ],
        storeFront: [
          {
            required: true,
            message: "店铺装潢图不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.storekeeperId = id;
      console.log("StoreAdd,storeKeeperId=", id);
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.$http
          .get(
            `${process.env.ADMIN_URL}/admin/classify/listClassifyNames`,
            {
              withCredentials: true,
            }
          )
          .then(({ data }) => {
              this.classifies = data;
          });
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http
            .post(
              process.env.URL_HEAD + `/store/store/save`,
              {
                storekeeperId: this.dataForm.storekeeperId,
                classifyId: this.dataForm.classifyId,
                storeName: this.dataForm.storeName,
                storeDesc: this.dataForm.storeDesc,
                storeAnno: this.dataForm.storeAnno,
                storeIcon: this.dataForm.storeIcon,
                backgroundUrl: this.dataForm.backgroundUrl,
                storeLocation: this.dataForm.storeLocation,
                storeAddress: this.dataForm.storeAddress,
                storePhone: this.dataForm.storePhone,
                storeStatus: 0,
                storeIntegrity: this.dataForm.storeIntegrity,
                businessLicense: this.dataForm.businessLicense,
                storeSign: this.dataForm.storeSign,
                storeFront: this.dataForm.storeFront,
              },
              { withCredentials: true }
            )
            .then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList", this.dataForm.storeId); //====================================================
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
        }
      });
    },
  },
};
</script>
